package com.ruoyi.test.bd;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.test.Key;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    // 在平台申请的APP_ID 详见 http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer
    private static final String APP_ID = "20221011001384917";
    private static final String SECURITY_KEY = "U0fHaYQ7ffTF6aBQYj_2";
    static List<String> listA = new ArrayList<>();
    public static void main(String[] args) throws UnsupportedEncodingException {
//        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
//
//        String query = "请指定参数processDefinitionId ";
//        System.out.println(api.getTransResult(query, "auto", "en"));

        aa();
        writeListToFile(listA, "D:\\Sh4\\ruoyi-plus-tdengine\\ruoyi-admin\\src\\test\\java\\com\\ruoyi\\test\\bd\\workflow.txt");
    }

    public static void writeListToFile(List<String> list, String filePath) {
        try (FileWriter writer = new FileWriter(filePath)) {
            for (String line : list) {


                writer.write(line + System.lineSeparator());
            }
            System.out.println("List内容已写入到文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void aa(){
        String filePath = "D:\\Sh4\\ruoyi-plus-tdengine\\ruoyi-admin\\src\\test\\java\\com\\ruoyi\\test\\out.txt"; // 替换为你的文件路径


        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String deal = deal(line);
                listA.add(deal);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String deal(String line) throws UnsupportedEncodingException {
        System.out.println(line); // 在这里对每一行内容进行处理，这里只是简单地输出到控制台
        String[] split = line.split("=");

        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        String query = split[1];
        if (!query.equals("null") && StringUtils.isNotBlank(query)){
            String transResult = api.getTransResult(query, "auto", "en");
            System.out.println(transResult);
            JSONObject jsonObject = JSONUtil.parseObj(transResult).getJSONArray("trans_result").getJSONObject(0);
            String str = jsonObject.getStr("dst");
            String s = str.replaceAll(" ", "_");
            String s1 = split[0] + s.toLowerCase().replaceAll(":","") + "=" + query;
            return s1;

        }

        return line;
    }
}
